package com.hooker.settings;

import android.content.Context;
import android.util.ArraySet;

import com.hooker.common.StringUtil;
import com.hooker.settings.Setting;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

public class SettingHelper {

    /**
     * @param pName
     * @param typeName
     * @param params
     * @return package_Hooker.file_(path, class, method)_start
     */
    public static String getBuildInfo(String pName, String typeName, String... params) {
        StringBuilder builder = new StringBuilder();
        builder.append(pName);
        builder.append("_" + typeName);
        if (params != null) {
            for (String param : params) {
                if (param != null && !"".equals(param)) {
                    builder.append("_" + param);
                }
            }
        }
        return builder.toString();
    }

    public static void removeInfo(Context context, String tag, String value) {
        List<String> values = getInfo(context, tag);

        if (values.contains(value)) {
            values.remove(value);
        }
        if (values.size() == 0) {
            Setting.put(context, tag, "");
        } else {
            StringBuilder builder = new StringBuilder();
            StringUtil.join(builder, values, Setting.SPLITSTR);
            Setting.put(context, tag, builder.toString());
        }
    }

    public static void putInfo(Context context, String tag, String value) {
        List<String> values = getInfo(context, tag);

        if (values.contains(value)) {
            return;
        }

        values.add(value);
        StringBuilder builder = new StringBuilder();
        StringUtil.join(builder, values, Setting.SPLITSTR);

        Setting.put(context, tag, builder.toString());
    }

    public static List<String> getInfo(Context context, String tag) {
        String values = Setting.get(context, tag);
        if (values != null && !"".equals(values)) {
            String[] data = values.split(Setting.SPLITSTR);

            return new ArrayList<>(Arrays.asList(data));
        }
        return new ArrayList<>();
    }

    /**
     * @param context
     * @param pgName
     * @param apps    注入的应用
     * @return
     */
    public static void inject(Context context, String pgName, String... apps) {
        JSONObject json;

        String values = Setting.getInjectValue(context);

        try {
            if (values == null || "".equals(values)) {
                json = new JSONObject();
            } else {
                json = new JSONObject(values);
            }

            if (apps == null || apps.length == 0) {
                json.remove(pgName);
            } else {
                injectToSetting(json, pgName, apps);
            }
            Setting.inject(context, json.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private static void injectToSetting(JSONObject json, String pgName, String... apps) {
        String injects = json.optString(pgName, null);

        Set<String> list = new ArraySet<>();

//        if (injects != null) {
//            list.addAll(Arrays.asList(injects.split(Setting.SPLITSTR)));
//        }

        if (apps != null && apps.length > 0) {
            for (String app : apps) {
                list.add(app);
            }
        }

        StringBuilder builder = new StringBuilder();
        StringUtil.join(builder, list, Setting.SPLITSTR);
        try {
            json.put(pgName, builder.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static List<String> getInjectValues(Context context, String pgName) {
        String values = Setting.getInjectValue(context);

        JSONObject json;
        try {
            if (values == null || "".equals(values)) {
                json = new JSONObject();
            } else {
                json = new JSONObject(values);
            }

            String list = json.optString(pgName, null);
            if (list != null) {
                return Arrays.asList(list.split(Setting.SPLITSTR));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return new ArrayList<>();
    }
}
